package wiki.hitime.stl.daq.web.controller.admin;

import wiki.hitime.stl.daq.bean.base.RespDTO;
import wiki.hitime.stl.daq.bean.dto.req.*;
import wiki.hitime.stl.daq.exception.ServiceException;
import wiki.hitime.stl.daq.service.admin.RuleManageService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zhangdaoping
 * @create 2019-07-24 10:57
 */
@RestController
@RequestMapping("/admin/rule")
public class RuleManageController {

    private RuleManageService ruleManageService;

    public RuleManageController(RuleManageService ruleManageService) {
        this.ruleManageService = ruleManageService;
    }

    /**
     * 获取执行规则列表
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/executeRule/listView")
    public RespDTO listRuleView(@Valid @RequestBody ExecuteRuleViewListReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.listExecuteRuleView(reqDTO));
    }

    /**
     * 添加执行任务
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     * @throws ServiceException
     */
    @PostMapping(value = "/executeTask/add")
    public RespDTO addExecuteTask(@Valid @RequestBody ExecuteTaskAddReqDTO reqDTO, BindingResult bindingResult) throws ServiceException {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        ruleManageService.addTask(reqDTO);
        return RespDTO.success();
    }

    /**
     * 获取执行规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/executeRule/get")
    public RespDTO getExecuteRule(@Valid @RequestBody ExecuteRuleGetReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.getExecuteRule(reqDTO));
    }

    /**
     * 获取采集规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/acquisitionRule/get")
    public RespDTO getExecuteRule(@Valid @RequestBody AcquisitionRuleGetReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.getAcquisitionRule(reqDTO));
    }

    /**
     * 获取检查规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/checkRule/get")
    public RespDTO getCheckRule(@Valid @RequestBody CheckRuleGetReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.getCheckRule(reqDTO));
    }

    /**
     * 获取检查规则集合
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/checkRule/list")
    public RespDTO listCheckRule(@Valid @RequestBody CheckRuleListReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.listCheckRule(reqDTO));
    }

    /**
     * 获取消息规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/msgRule/get")
    public RespDTO getMsgRule(@Valid @RequestBody MsgRuleGetReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(ruleManageService.getMsgRule(reqDTO));
    }

    /**
     * 修改执行规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     * @throws ServiceException
     */
    @PostMapping(value = "/executeRule/update")
    public RespDTO updateExecuteRule(@Valid @RequestBody ExecuteRuleUpdateReqDTO reqDTO, BindingResult bindingResult) throws ServiceException {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.updateExecuteRule(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则修改不成功");
    }

    /**
     * 修改采集规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     * @throws ServiceException
     */
    @PostMapping(value = "/acquisitionRule/update")
    public RespDTO updateAcquisitionRule(@Valid @RequestBody AcquisitionRuleUpdateReqDTO reqDTO, BindingResult bindingResult) throws ServiceException {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.updateAcquisitionRule(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则修改不成功");
    }

    /**
     * 修改检查规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     * @throws ServiceException
     */
    @PostMapping(value = "/checkRule/update")
    public RespDTO updateCheckRule(@Valid @RequestBody CheckRuleListAddUpdateReqDTO reqDTO, BindingResult bindingResult) throws ServiceException {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.updateCheckRule(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则修改不成功");
    }

    /**
     * 修改消息规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     * @throws ServiceException
     */
    @PostMapping(value = "/msgRule/update")
    public RespDTO updateMsgRule(@Valid @RequestBody MsgRuleUpdateReqDTO reqDTO, BindingResult bindingResult) throws ServiceException {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.updateMsgRule(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则修改不成功");
    }

    /**
     * 修改执行规则状态
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/executeRule/updateAction")
    public RespDTO updateMsgRule(@Valid @RequestBody ExecuteRuleUpdateActionReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.updateExecuteAction(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则修改不成功");
    }

    /**
     * 删除执行规则
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/executeTask/delete")
    public RespDTO updateMsgRule(@Valid @RequestBody ExecuteRuleDeleteReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Boolean result = ruleManageService.deleteTask(reqDTO);
        return result ? RespDTO.success() : RespDTO.fail("规则删除不成功");
    }
}
